﻿using RobotPratolSolution.ViewModels;
using System.Windows;
using System.Windows.Controls;

namespace RobotPratolSolution.Pages
{
    /// <summary>
    /// TaskMange.xaml 的交互逻辑
    /// </summary>
    public partial class TaskMange : UserControl
    {
        TaskListViewModel taskListViewModel { get; set; }

        public TaskMange()
        {
            taskListViewModel = new TaskListViewModel();

            InitializeComponent();
        }



        private void CreateBtn_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(JobName.Text))
                taskListViewModel.AddJobInfo(JobName.Text);
        }

        private void DeleteBtn_Click(object sender, RoutedEventArgs e)
        {
            var item = TaskListBox.SelectedIndex;
            if (item >= 0)
                taskListViewModel.DeleteTask(TaskListBox.SelectedIndex);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            taskListViewModel.RefreshTask();
            TaskListBox.ItemsSource = taskListViewModel.JobModels;
        }

        private void StartBtn_Click(object sender, RoutedEventArgs e)
        {
            int index = TaskListBox.SelectedIndex;
            taskListViewModel.StartJob(index);
        }

        Task WorkTask;
        CancellationTokenSource tokenSource = new CancellationTokenSource();

        private void PauseBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void StopBtn_Click(object sender, RoutedEventArgs e)
        {
            int index = TaskListBox.SelectedIndex;
            taskListViewModel.StopJob();
        }

        private void ModifyBtn_Click(object sender, RoutedEventArgs e)
        {
            var item = TaskListBox.SelectedIndex;
            if (item >= 0)
                taskListViewModel.UpdateJobProperty(item, JobName.Text);
        }

        private void TaskListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = TaskListBox.SelectedIndex;
            if (index >= 0)
                JobName.Text = taskListViewModel.JobModels[index].Name;
        }

        private void RefreshBtn_Click(object sender, RoutedEventArgs e)
        {
            taskListViewModel.RefreshTask();
            TaskListBox.ItemsSource = taskListViewModel.JobModels;
        }
    }
}
